package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.LogradouroMODEL;

/**
 *
 * @author WELISSON
 */
public class LogradouroDAO extends GenericDAO{
    public List<LogradouroMODEL> findByArgument(String argumento, String pesquisa) throws SQLException{
        
        List<LogradouroMODEL> logradouros = new ArrayList<LogradouroMODEL>();
        
        
        String select = "SELECT * FROM logradouro WHERE "+argumento+" = ? ";
        
        PreparedStatement stmt = 
            getConn().prepareStatement(select);
        
        stmt.setInt(1, Integer.parseInt(pesquisa));
        ResultSet rs = stmt.executeQuery();
        
        while (rs.next()) {
            LogradouroMODEL logradouro = new LogradouroMODEL();
            logradouro.setCep(rs.getString("log_cep"));
            logradouro.setEndereco(rs.getString("log_endereco"));
            logradouro.setBairro(rs.getString("log_bairro"));
            logradouro.setCidade(rs.getString("log_cidade"));
            logradouro.setUf(rs.getString("log_uf"));
            logradouros.add(logradouro);
        }
 
        rs.close();
        stmt.close();
        conn.close();
        
        
        return logradouros;
    }
    
    public LogradouroMODEL getLogradouro(int cep) throws SQLException{
        
        String select = "SELECT * FROM logradouro WHERE log_cep = ? ";
        PreparedStatement ps = conn.prepareStatement(select);
        ps.setObject(1, cep);
        ResultSet rs = ps.executeQuery();
        LogradouroMODEL log = new LogradouroMODEL();
        while(rs.next()){
            log.setCep(rs.getString("log_cep"));
            log.setEndereco(rs.getString("log_endereco"));
            log.setBairro(rs.getString("log_bairro"));
            log.setCidade(rs.getString("log_cidade"));
            log.setUf(rs.getString("log_uf"));
        }
        return log;
        
    }
    
}
